﻿using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Inovout.RedictUrls;
using Inovout.Runtime;
using Inovout.Memberships.Services;
using Inovout.Account.Web.Models;
using Inovout.Security.Services;
using Inovout.Security;
using Inovout.Memberships;

namespace Inovout.Account.Web.Controllers
{
    [AllowAnonymous]
    public class HelpController : BaseController
    {
        public ActionResult ForgetOptions(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        public ActionResult FindPassWord(FindPasswordInfo findPasswordInfo, string email, string returnUrl)
        {
            ViewBag.Email = email;
            //FormsUserCredentialService formsUserCredentialService = Inovout.ComponentRegistry.Resolve<FormsUserCredentialService>();
            //FormsUserCredential formsUserCredential = formsUserCredentialService.FindByEmail(email);
            //if (formsUserCredential == null)
            //{
            //    ModelState.AddModelError("Email", "该电子邮件不正确");
            //    return View("ForgetOptions", findPasswordInfo);
            //}
            Inovout.Memberships.IMembershipUser membershipUser = Inovout.ComponentRegistry.Resolve<MembershipUserService>().FindByUserNameOrEmail(email);
            if (membershipUser == null || membershipUser.Status == MembershipUserStatus.Inactivate)
            {
                ModelState.AddModelError("Email", "该电子邮件不正确");
                return View("ForgetOptions", findPasswordInfo);
            }
            RedirctUrl entity = new RedirctUrl();
            entity.OrginalUrl = (new Uri(this.Request.Url, Url.Action("FindPassWord", "FormsUser", new { Email = email, ReturnUrl = returnUrl }))).AbsoluteUri;
            entity.FailedUrl = (new Uri(this.Request.Url, Url.Action("Message", new { messageType = MessageType.Failed }))).AbsoluteUri;
            string guid = Guid.NewGuid().ToString("N");
            entity.ActionUrl = (new Uri(this.Request.Url, Url.Action("Index", "Go", new { key = guid }))).AbsoluteUri;
            Inovout.ComponentRegistry.Resolve<IRedictUrlService>().Save(entity);
            string plate = @"要重设您的 3Cloud 帐户 $(email) 的密码，请点击以下链接："
                + Environment.NewLine
                + Environment.NewLine + "$(url)"
                + Environment.NewLine
                + Environment.NewLine + "如果点击以上链接没有反应，请将该网址复制并粘贴到新的浏览器窗口中。"
                + Environment.NewLine
                + Environment.NewLine
                + "如果您是误收到这封邮件，则可能是因为其他用户在尝试重设密码时误输入了您的电子"
                + Environment.NewLine + "邮件地址。如果您并未发过此请求，则可以放心地忽略此电子邮件，并且无需进一步采"
                + Environment.NewLine + "取任何操作。"
                + Environment.NewLine
                + Environment.NewLine
                + Environment.NewLine
                + "此致"
                + Environment.NewLine + "3Cloud 帐户小组敬上";
            Dictionary<string, string> contents = new Dictionary<string, string>();
            contents.Add("email", email);
            contents.Add("url", entity.ActionUrl);
            string subject = "3cloud 找回密码";
            Inovout.ComponentRegistry.Resolve<IRedictUrlService>().SendMail(plate, contents, subject);
            return View();
        }

        public ActionResult LoginEmail(string email)
        {
            string emailDomain = email.Split('@')[1].ToString().ToLower();
            string emailUrl = "http://mail." + emailDomain;
            return Redirect(emailUrl);
        }
    }
}